#ifndef REDIS_UTIL_H
#define REDIS_UTIL_H
//#include <vector>
//#include <set>
//#include <map>
//#include "player_redis_info.pb-c.h"
#include "redis_client.h"
#include <protobuf-c/protobuf-c.h>
// class AutoReleaseRedisPlayer
// {
// public:
// 	AutoReleaseRedisPlayer();
// 	~AutoReleaseRedisPlayer();
// 	void set(PlayerRedisInfo* r);
// private:
// 	PlayerRedisInfo *pointer;
// };

// class AutoReleaseBatchRedisPlayer
// {
// public:
// 	AutoReleaseBatchRedisPlayer();
// 	~AutoReleaseBatchRedisPlayer();

// 	void push_back(PlayerRedisInfo *player);
// private:
// 	std::vector<PlayerRedisInfo *> pointer_vec;
// };

typedef void (*free_func)(void *message, ProtobufCAllocator *allocator);
typedef void *(*unpack_func)(ProtobufCAllocator *allocator, size_t len, const uint8_t *data);
class AutoReleaseProtobufStruct
{
public:
	AutoReleaseProtobufStruct(void *m, ProtobufCAllocator *a, free_func f_handler, unpack_func u_handler)
	{
		message = m;
		free_handler = f_handler;
		unpack_handler = u_handler;
		allocator = a;
	}
	~AutoReleaseProtobufStruct()
	{
		if (free_handler)
		{
			if (message)
				(*free_handler)(message, allocator);
			for (std::vector<void *>::iterator ite = pointer_vec.begin();
				 ite != pointer_vec.end(); ++ite)
			{
				(*free_handler)(*ite, allocator);				
			}
		}
	}
 	void set(void* r);
	void push_back(void *r);
	
	unpack_func unpack_handler;
	ProtobufCAllocator *allocator;
	void *      message;
 	std::vector<void *> pointer_vec;	
	free_func   free_handler;
};

#define DEF_AUTO(FUNC, a) AutoReleaseProtobufStruct a(NULL, NULL, (free_func)FUNC##__free_unpacked, (unpack_func)FUNC##__unpack);
void *get_redis_pbdata(uint64_t player_id, const char *player_key, CRedisClient &rc, AutoReleaseProtobufStruct &_pool);
int get_more_pbdata(std::set<uint64_t> &player_ids, std::map<uint64_t, void *> &redis_players,
	const char *player_key, CRedisClient &rc, AutoReleaseProtobufStruct &_pool);

// PlayerRedisInfo *get_redis_player(uint64_t player_id, char *player_key, CRedisClient &rc, AutoReleaseRedisPlayer &_pool);
// PlayerRedisInfo *get_redis_player(uint64_t player_id, char *player_key, CRedisClient &rc, AutoReleaseBatchRedisPlayer &_pool);
// int get_more_redis_player(std::set<uint64_t> &player_ids, std::map<uint64_t, PlayerRedisInfo*> &redis_players,
// 	char *player_key, CRedisClient &rc, AutoReleaseBatchRedisPlayer &_pool);

#endif /* REDIS_UTIL_H */

